#include "MessageQueueTest.h"

void MessageQueueTest::ProcessMessage(const Message& message)
{
	//printf("Process message [%d] at thread [%d]\n", msg.id, std::this_thread::get_id());
	printf("Process message [%d]\n", message.id);
	switch (message.id)
	{
	case MSG_START:
	{
		SendingMessage(MSG_SUSPEND, 1231);
		break;
	}

	case MSG_SUSPEND:
	{
		SendingMessage(MSG_CANCEL, 3222);
		break;
	}

	case MSG_CANCEL:
	{		
		SendingMessage(MSG_EXIT, 1312);
		break;
	}

	case MSG_EXIT:
	{		
		SendingMessage(MSG_ERR, 15000);
		break;
	}

	case MSG_ERR:
	{
		SendingMessage(MSG_START, 5000);
		break;
	}
	default:
		printf("Receiving other messages....\n");
		break;
	}
}

void MessageQueueTest::HandleMessage(const Message& message)
{
	printf("Handle message [%d]\n", message.id);
}

void MessageQueueTest::SendingMessage(int i, int delay)
{
	printf("Process message [%d] after [%d] milliseconds.\n", i, delay);
	looper.enquee(i, delay, func);
}

void MessageQueueTest::TestLoop()
{
	SendingMessage(MSG_START, 0);
}

void MessageQueueTest::TestDelay()
{
	int times[5] = {6000, 1000, 3000, 2000, 4000};
	printf("Handle message [%d] after [%d] milliseconds.\n", MSG_START, times[0]);
	looper.enquee(MSG_START, times[0], callback);
	printf("Handle message [%d] after [%d] milliseconds.\n", MSG_SUSPEND, times[1]);
	looper.enquee(MSG_SUSPEND, 1000, callback);
	printf("Handle message [%d] after [%d] milliseconds.\n", MSG_CANCEL, times[2]);
	looper.enquee(MSG_CANCEL, 3000, callback);
	printf("Handle message [%d] after [%d] milliseconds.\n", MSG_EXIT, times[3]);
	looper.enquee(MSG_EXIT, 2000, callback);
	printf("Handle message [%d] after [%d] milliseconds.\n", MSG_ERR, times[4]);
	looper.enquee(MSG_ERR, 4000, callback);
}

void MessageQueueTest::ClearMessage()
{
	looper.removeAllMessages();
}

void MessageQueueTest::Start()
{	
	TestDelay();	
	TestLoop();
}

void MessageQueueTest::Stop()
{
	looper.quit();
}
